﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace HeritageAcademy.Library.Controls
{
    /// <summary>
    /// User tile control class.
    /// Based on code from Thirteen23.
    /// </summary>
    public partial class UserTile
    {
        #region Parameters
        private string userId = string.Empty;
        private string _userName = string.Empty;
        private string _userTitle = string.Empty;
        private string _password = string.Empty;
        private bool isLoggedIn;
        private ImageSource _imgUserIcon;
        #endregion

        #region Ctor
        public UserTile() 
		{
            InitializeComponent();
            UpdateDetails();

            Loaded += UserTile_Loaded;
		}
        #endregion

        #region Handlers
        /// <summary>
        /// User tile loaded event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event arguments</param>
        void UserTile_Loaded(object sender, RoutedEventArgs e)
        {
            MouseEnter += UserTile_MouseEnter;
            MouseLeave += UserTile_MouseLeave;
        }
 
        /// <summary>
        /// User tile mouse enter event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Mouse event arguments</param>
        void UserTile_MouseEnter(object sender, MouseEventArgs e)
        {
            if (isLoggedIn) return;
            var slideOut = FindResource("slideOut") as Storyboard;
            if (slideOut != null) slideOut.Begin(this);
        }

        /// <summary>
        /// User tile mouse leave event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Mouse event arguments</param>
        void UserTile_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isLoggedIn) return;
            var slideIn = FindResource("slideIn") as Storyboard;
            if (slideIn != null) slideIn.Begin(this);
        }
        #endregion

        #region Properties
        /// <summary>
        /// User Id
        /// </summary>
        public string UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        /// <summary>
        /// User name.
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set {
                if (value == _userName) return;
                _userName = value;
                UpdateDetails();
                OnUserNameChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> UserNameChanged;
        protected virtual void OnUserNameChanged(EventArgs args)
        {
            var handler = UserNameChanged;
            if (handler != null) handler(this, args);
        }

        /// <summary>
        /// User icon image.
        /// </summary>
        public ImageSource UserIcon
        {
            get { return _imgUserIcon; }
            set
            {
                if (value == _imgUserIcon) return;
                _imgUserIcon = value;
                UpdateDetails();
                OnUserIconChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> UserIconChanged;
        protected virtual void OnUserIconChanged(EventArgs args)
        {
            var handler = UserIconChanged;
            if (handler != null) handler(this, args);
        }

        /// <summary>
        /// User title.
        /// <example>Student, tutor, mam, dad, superman, etc.</example>
        /// </summary>
        public string UserTitle
        {
            get { return _userTitle; }
            set
            {
                if (value == _userTitle) return;
                _userTitle = value;
                UpdateDetails();
                OnUserTitleChanged(EventArgs.Empty);
            }
        }
        
        public event EventHandler<EventArgs> UserTitleChanged;
        protected virtual void OnUserTitleChanged(EventArgs args)
        {
            var handler = UserTitleChanged;
            if (handler != null) handler(this, args);
        }

        /// <summary>
        /// Password string.
        /// </summary>
        public string Password
        {
            get { return _password; }
            set
            {
                if (value == _password) return;
                _password = value;
                OnPasswordChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> PasswordChanged;
        protected virtual void OnPasswordChanged(EventArgs args)
        {
            var handler = PasswordChanged;
            if (handler != null) handler(this, args);
        }

        /// <summary>
        /// Current user authorization status.
        /// </summary>
        public bool IsLoggedIn
        {
            get { return isLoggedIn; }
            set
            {
                if (value == isLoggedIn) return;
                isLoggedIn = value;
                UpdateDetails();
                OnIsLoggedInChangedChanged(EventArgs.Empty);
            }
        }

        public event EventHandler<EventArgs> IsLoggedInChanged;
        protected virtual void OnIsLoggedInChangedChanged(EventArgs args)
        {
            var handler = IsLoggedInChanged;
            if (handler != null) handler(this, args);
        }

        /// <summary>
        /// Password box.
        /// </summary>
        public PasswordBox PasswordInput
        {
            get { return password; }
            set { password = value; }
        }

        /// <summary>
        /// Password box border.
        /// </summary>
        public Border PasswordBorder
        {
            get { return passwordBorder; }
            set { passwordBorder = value; }
        }

        /// <summary>
        /// Update visual controls with private property values
        /// </summary>
        public void UpdateDetails()
        {
            lbUserName.Content = _userName;
            lbPosition.Content = _userTitle;
            imgUser.Source = _imgUserIcon;
            logoutButton.Visibility = (isLoggedIn) ? Visibility.Visible : Visibility.Hidden;
        }
        #endregion
    }
}